import java.util.ArrayList;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author sergi
 */
public class Texto {
    private ArrayList<String> t;
    private int longitud;

    public Texto() {
        t = new ArrayList<String>();
        longitud = 0;
    }

    public ArrayList<String> gettexto() {
        return t;
    }

    public void settexto(ArrayList<String> tex) {
        t = tex;
        longitud = t.size();
    }

    public String getlinea(int lin) {
        return t.get(lin);
    }

    public void anadirlinea(String a, int lin) {
        t.add(lin, a);
        ++longitud;
    }

    public void borrarlinea(int lin) {
        t.remove(lin);
        --longitud;
    }

    private boolean escaracter(char a) {
        if (a == '.' || a == ',' || a == ' ' || a == '?' || a == '!' || a == ':' || a == ';') return true;
        return false;
    }

    public ArrayList<String> getpalinc(String nombredic, String d, ControladorDiccionario cd) {
        int l = d.length();
        ArrayList<String> inc = new ArrayList<String>();
        int i = 0;
        while(i < l) {
            String aux = "";
            int j = i;
            while (i < l && !escaracter(d.charAt(i))) {
                 ++i;
            }
            if(i - j > 0) {
                aux = d.substring(j, i);
                String aux2 = aux;
                if(aux.charAt(0) >= 'A') aux = aux.toLowerCase();
                if (!cd.ExisteEnDiccionario(nombredic, aux)) inc.add(aux2);
            }  
            ++i;
        }
        return inc;
    }

    public ArrayList<StringDouble> getkposibles(String palinc, Reglas r, Trie tr, Algoritmo a, int k) throws ExcepcionVacio {
        boolean mayus = false;
        if(palinc.charAt(0) < 'a') mayus = true;
        if(mayus == true) palinc = palinc.toLowerCase();
        ArrayList<StringDouble> a1 = a.cand(palinc.toCharArray(), tr, r, k);
        if (mayus == true) {
            for(int i = 0; i < a1.size(); ++i)  {
                char[] aux = a1.get(i).s.toCharArray();
                aux[0] = (char) (aux[0] + 'A' - 'a');
                StringDouble aux2 = new StringDouble();
                aux2.s = String.valueOf(aux);
                aux2.d = a1.get(i).d;
                a1.set(i,aux2);
            }
        }
        return a1;
    }

    public void cambiarpaltodo(String inc, String cor) {
          ArrayList<String> lineas = t;
          cor.toLowerCase();
          int i;
          for(i = 0; i < lineas.size(); ++i) {
              int k;
              for(k = 0; k < lineas.get(i).length(); ++k) {
                    int j = k;
                    while ((k < lineas.get(i).length()) && !escaracter(lineas.get(i).charAt(k))) {
                        ++k;
                    }
                    if(k - j > 0) {
                        String aux;
                        aux = lineas.get(i).substring(j, k);
                        if(aux.equalsIgnoreCase(inc)) {
                            String cor3;
                            char[] cor2 = cor.toCharArray();
                            if(aux.charAt(0) < 'a') {
                                  if(cor2[0] >= 'a') cor2[0] = (char) (cor2[0] + 'A' - 'a');
                            }
                            else {
                                if(cor2[0] < 'a') cor2[0] = (char) (cor2[0] + 'a' - 'A');
                            }
                            cor3 = String.valueOf(cor2);
                            lineas.set(i, lineas.get(i).substring(0, j).concat(cor3.concat(lineas.get(i).substring(k, lineas.get(i).length()))));
                            k += cor3.length() - inc.length();
                        }
                    }
              }
          }
          t = lineas;
    }

    public void cambiarpalsola(String inc, String cor, int rep) {
          ArrayList<String> lineas = t;
          int i;
          int repact = 1;
          boolean b = false;
          for(i = 0; i < lineas.size() && b == false; ++i) {
              int k;
              for(k = 0; k < lineas.get(i).length() && b == false; ++k) {
                    int j = k;
                    while ((k < lineas.get(i).length()) && !escaracter(lineas.get(i).charAt(k))) {
                        ++k;
                    }
                    if(k - j > 0) {
                        String aux;
                        aux = lineas.get(i).substring(j, k);
                        if(aux.equalsIgnoreCase(inc) && repact == rep) {
                            lineas.set(i, lineas.get(i).substring(0, j).concat(cor.concat(lineas.get(i).substring(k, lineas.get(i).length()))));
                            b = true;
                        }
                        else if(aux.equals(inc) && repact != rep) ++repact;
                    }
              }
          }
          t = lineas;
    }

    public int longitud() {
        return longitud;
    }

}
